package flown.game.mine;



import javax.swing.*;
import java.awt.event.*;
import java.util.Objects;


/**
 * @author flown
 */


public class GameWindow
{
    private static final String WINDOW_MAIN_TITLE = "MineGame";
    private static final String WINDOW_ABOUT_TITLE = "About";

    private static int WINDOW_MAIN_WIDTH = 800;
    private static int WINDOW_MAIN_HEIGHT = 600;
    private static final int WINDOW_ABOUT_WIDTH = 320;
    private static final int WINDOW_ABOUT_HEIGHT = 180;

    private static final String WINDOW_ABOUT_TEXT = "Made By Flown";

    private static final String MENU_GAME_TEXT = "Game";
    private static final String MENU_SETTING_TEXT = "Setting";
    private static final String MENU_HELP_TEXT = "Help";
    private static final int MENU_GAME_INDEX = 0;
    private static final int MENU_SETTING_INDEX = 1;
    private static final int MENU_HELP_INDEX = 2;

    private static final String MENU_GAME_ITEM_NETWORK_TEXT = "Network";
    private static final String MENU_GAME_ITEM_EXIT_TEXT = "Exit";
    private static final String MENU_HELP_ITEM_ABOUT_TEXT = "About";

    private final flown.window.Window window;
    private final flown.window.MenuBar menu_bar;
    private final flown.window.graphics.Scene scene;


    /*Container*/
    private final flown.window.graphics.ui.Container container = new flown.window.graphics.ui.Container();
    private final flown.window.graphics.ui.Button button_start;

    //信息显示
    private final JScrollPane view_pane = new JScrollPane();
    private final JTextArea information_view = new JTextArea();
    private final JTextField text_field = new JTextField();
    private final JButton send_button = new JButton("Send");


    private flown.game.mine.Game.State game_state = flown.game.mine.Game.State.THINKING;

    public GameWindow()
    {
        flown.game.mine.GameWindow that = this;
        this.window = new flown.window.Window(flown.game.mine.GameWindow.WINDOW_MAIN_TITLE, flown.game.mine.GameWindow.WINDOW_MAIN_WIDTH, flown.game.mine.GameWindow.WINDOW_MAIN_HEIGHT);
        this.menu_bar = new flown.window.MenuBar();

        //布局
        this.window.SetLayout(null);

        //菜单栏添加菜单，菜单添加菜单项
        this.menu_bar.AddMenus(flown.game.mine.GameWindow.MENU_GAME_TEXT,
                flown.game.mine.GameWindow.MENU_SETTING_TEXT,
                flown.game.mine.GameWindow.MENU_HELP_TEXT
        );
        this.menu_bar.AddMenuItems(flown.game.mine.GameWindow.MENU_GAME_INDEX,
                flown.game.mine.GameWindow.MENU_GAME_ITEM_NETWORK_TEXT,
                flown.game.mine.GameWindow.MENU_GAME_ITEM_EXIT_TEXT
        );
        this.menu_bar.AddMenuItems(flown.game.mine.GameWindow.MENU_HELP_INDEX,
                flown.game.mine.GameWindow.MENU_HELP_ITEM_ABOUT_TEXT
        );


        //网络联机菜单项
        this.menu_bar.MenuItemAddListener(flown.game.mine.GameWindow.MENU_GAME_INDEX,
                this.menu_bar.GetMenuItemIndex(flown.game.mine.GameWindow.MENU_GAME_INDEX, flown.game.mine.GameWindow.MENU_GAME_ITEM_NETWORK_TEXT),
                new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        flown.window.Dialog dialog_network = new flown.window.Dialog(that.window);
                    }
                }
        );
        //退出游戏菜单项
        this.menu_bar.MenuItemAddListener(flown.game.mine.GameWindow.MENU_GAME_INDEX,
                this.menu_bar.GetMenuItemIndex(flown.game.mine.GameWindow.MENU_GAME_INDEX, flown.game.mine.GameWindow.MENU_GAME_ITEM_EXIT_TEXT),
                new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e) {System.exit(0);}
                }
        );
        //关于菜单项
        this.menu_bar.MenuItemAddListener(flown.game.mine.GameWindow.MENU_HELP_INDEX,
                this.menu_bar.GetMenuItemIndex(flown.game.mine.GameWindow.MENU_HELP_INDEX, flown.game.mine.GameWindow.MENU_HELP_ITEM_ABOUT_TEXT),
                new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        flown.window.Dialog dialog_about = new flown.window.Dialog(
                                that.window,
                                flown.game.mine.GameWindow.WINDOW_ABOUT_TITLE,
                                flown.game.mine.GameWindow.WINDOW_ABOUT_WIDTH,
                                flown.game.mine.GameWindow.WINDOW_ABOUT_HEIGHT
                        );


                        flown.window.graphics.Scene scene = new flown.window.graphics.Scene(dialog_about);
                        scene.SetRenderEvent(renderer2D -> {
                            renderer2D.SetFontSize((float)scene.getWidth() / 10);

                            int text_width = renderer2D.GetTextWidth(flown.game.mine.GameWindow.WINDOW_ABOUT_TEXT);
                            int text_height = renderer2D.GetTextHeight();
                            renderer2D.DrawText(flown.game.mine.GameWindow.WINDOW_ABOUT_TEXT,
                                    scene.getWidth() / 2 - text_width / 2,
                                    scene.getHeight() / 2 + text_height / 2
                                    );
                        });
                    }
                }
        );
        this.menu_bar.AddInWindow(this.window);//将菜单栏添加至窗口


        /*Scene*/
        this.scene = new flown.window.graphics.Scene(this.window);

        /*Container*/
        this.button_start = new flown.window.graphics.ui.Button("Start",
                that.scene.getWidth() * 3 / 8,
                that.scene.getHeight() / 2,
                that.scene.getWidth() / 4,
                that.scene.getHeight() / 10
        );
        this.button_start.AddInContainer(container);


        /*事件设置*/
        container.InsertIntoScene(this.scene);
        this.scene.SetRenderEvent(renderer -> {

            if (this.game_state == flown.game.mine.Game.State.THINKING)
            {
                //画游戏标题
                renderer.SetFontSize((float)that.scene.getWidth() / 10);
                renderer.DrawRect(
                        that.scene.getWidth() / 4,
                        that.scene.getHeight() / 10,
                        that.scene.getWidth() / 2,
                        that.scene.getHeight() / 5
                );
                renderer.DrawText(
                        flown.game.mine.GameWindow.WINDOW_MAIN_TITLE,
                        that.scene.getWidth() / 4,
                        that.scene.getHeight() / 20 * 5
                );

            }
            else if (this.game_state == flown.game.mine.Game.State.GAMING)
            {

            }
            else if (this.game_state == flown.game.mine.Game.State.PAUSE)
            {

            }

        });




        this.information_view.setEditable(false);
        this.information_view.setLineWrap(true);//自动换行
        this.information_view.setWrapStyleWord(true);
        this.information_view.setFont(this.information_view.getFont().deriveFont(15f));
        this.view_pane.setViewportView(this.information_view);

        this.text_field.setFont(this.text_field.getFont().deriveFont(18f));
        this.text_field.setLocation(0,0);

        this.window.Add(this.view_pane);
        this.window.Add(this.text_field);
        this.window.Add(this.send_button);


        //button listener
        send_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String text = that.text_field.getText();
                if (Objects.equals(text, ""))
                    return;
                that.text_field.setText("");
                information_view.append("You:\"" + text + "\"\n");
            }
        });


        //Listen Resize And KeyPressed of Text Field
        this.window.GetFrame().addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent e)
            {
                flown.game.mine.GameWindow.WINDOW_MAIN_WIDTH = e.getComponent().getWidth();
                flown.game.mine.GameWindow.WINDOW_MAIN_HEIGHT = e.getComponent().getHeight();

                //更新组件Position
                that.ComponentPositionUpdate();
            }

            @Override public void componentMoved(ComponentEvent e) {}
            @Override public void componentShown(ComponentEvent e) {}
            @Override public void componentHidden(ComponentEvent e) {}
        });

        text_field.addKeyListener(new KeyListener() {
            @Override public void keyTyped(KeyEvent e) {}
            @Override public void keyPressed(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_ENTER)
                    send_button.doClick();
            }
            @Override public void keyReleased(KeyEvent e) {}
        });


        this.ComponentPositionUpdate();
    }

    /**
     * 组件position更新
     */
    private void ComponentPositionUpdate()
    {
        final int interval = 10;///< 间隔

        //the component of window
        this.scene.setBounds(
                0,
                0,
                flown.game.mine.GameWindow.WINDOW_MAIN_WIDTH / 4 * 3,
                flown.game.mine.GameWindow.WINDOW_MAIN_HEIGHT / 6 * 5
        );
        this.view_pane.setBounds(
                this.scene.getWidth() + interval,
                0,
                flown.game.mine.GameWindow.WINDOW_MAIN_WIDTH / 4 - interval * 3,
                flown.game.mine.GameWindow.WINDOW_MAIN_HEIGHT * 5 / 6
        );
        this.text_field.setBounds(
                0,
                this.scene.getHeight() + interval,
                flown.game.mine.GameWindow.WINDOW_MAIN_WIDTH / 8 * 7,
                flown.game.mine.GameWindow.WINDOW_MAIN_HEIGHT / 12 - interval * 2
        );
        this.send_button.setBounds(
                text_field.getWidth(),
                this.scene.getHeight() + interval,
                flown.game.mine.GameWindow.WINDOW_MAIN_WIDTH / 8 - interval,
                flown.game.mine.GameWindow.WINDOW_MAIN_HEIGHT / 12 - interval * 2
        );

        //the component of container
        this.button_start.SetBounds(
                this.scene.getWidth() * 3 / 8,
                this.scene.getHeight() / 2,
                this.scene.getWidth() / 4,
                this.scene.getHeight() / 10
        );
    }


}








